package com.situ.myplayer.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.situ.myplayer.R;
import com.situ.myplayer.activity.MainActivity;
import com.situ.myplayer.activity.MusicPlayerActivity;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

/**
 * Created by Gao on 2024/6/24.
 */

public class FileAdapter extends BaseAdapter{
    //当前目录要显示的文件和文件夹
    private File[] mFiles;
    private MainActivity mMainActivity;

    public FileAdapter(MainActivity mainActivity) {
        this.mMainActivity = mainActivity;
    }

    /**
     *
     * @param currentDirectory  当前要展示的目录
     */
    public void updatePath(File currentDirectory) {
        //File在Java里面既可以表示文件也可以表示文件夹
        //mFiles = currentDirectory.listFiles();
        //过滤掉目录里面隐藏的文件，这些隐藏的文件“.”开头
        mFiles = currentDirectory.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return !pathname.getName().startsWith(".");
            }
        });
        Collections.sort(Arrays.asList(mFiles), new Comparator<File>() {
            @Override
            public int compare(File file1, File file2) {
                if (file1.isDirectory() && file2.isFile()) {
                    return -1;
                } else if (file1.isFile() && file2.isDirectory()) {
                    return 1;
                } else {
                    //file1和file2要么同时是文件夹要么同时是文件
                    return file1.getName().compareTo(file2.getName());
                }
            }
        });


        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mFiles.length;
    }

    @Override
    public Object getItem(int position) {
        return mFiles[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //实现Data----list_item布局的绑定
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 拿到list_item这个布局
        ViewHolder viewHolder = new ViewHolder();
        //第一次
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) mMainActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_item, null);
            viewHolder.ivFileIcon = (ImageView) convertView.findViewById(R.id.iv_file_icon);
            viewHolder.tvFileName = (TextView) convertView.findViewById(R.id.tv_file_name);
            //第一次保存起来
            convertView.setTag(viewHolder);
        } else {
            //如果不是第一次，从缓存中直接取出
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //绑定数据
        final File file = mFiles[position];
        final String fileName = file.getName();
        viewHolder.tvFileName.setText(fileName);
        if (file.isDirectory()) {//当前是文件夹
            viewHolder.ivFileIcon.setBackgroundResource(R.drawable.icon_folder);
        } else {//当前是文件
            //aa.mp3   bb.mp4
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1); // mp3
            if ("mp3".equalsIgnoreCase(suffix)) {
                viewHolder.ivFileIcon.setBackgroundResource(R.drawable.icon_audio);
            } else if ("mp4".equalsIgnoreCase(suffix)) {
                viewHolder.ivFileIcon.setBackgroundResource(R.drawable.icon_video);
            } else {
                viewHolder.ivFileIcon.setBackgroundResource(R.drawable.icon_file);
            }

        }

        //绑定点击事件
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (file.isDirectory()) {//当前点击的是文件夹
                    mMainActivity.updateCurrentDirectory(file);
                    updatePath(file);
                } else {//点击的是文件
                    String suffix = fileName.substring(fileName.lastIndexOf(".") + 1); // mp3
                    if ("mp3".equalsIgnoreCase(suffix)) {
                        Toast.makeText(mMainActivity, "mp3: " + fileName, Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(mMainActivity, MusicPlayerActivity.class);
                        intent.putExtra("path", file.getAbsolutePath());
                        mMainActivity.startActivity(intent);
                    } else if ("mp4".equalsIgnoreCase(suffix)) {

                    } else {
                    }
                }
            }
        });

        return convertView;
    }

    //封装list_item中的数据
    public class ViewHolder{
        public ImageView ivFileIcon;
        public TextView tvFileName;
    }





}
